import { GlobalTypes } from "../type";
import { IGlobal, IAction } from "./global.type";

const initState: IGlobal = {
    userInfo: {},//登录用户信息
    token: '',//token
    loadingCount: 0,//正在请求的数量
    tenantInfo: {},//租户信息
    tenant: '',//租户代码
    routes: [],//用户路由权限
    homeRoute: {},//第一个页面地址信息
    permissions: [],//用户按钮级权限
    flatRoutes: [],//打平路由
    sideMenuRoutes: [],//顶部菜单布局时侧面菜单内容
}

export const Global = (state: IGlobal = initState, action: IAction) => {
    let { type, data } = action
    switch (type) {
        case GlobalTypes.UPDATE_USERINFO:
            return {
                ...state,
                userInfo: data
            }
        case GlobalTypes.UPDATE_TOKEN:
            return {
                ...state,
                token: data
            }
        case GlobalTypes.SHOW_LOADING:
            return {
                ...state,
                loadingCount: state.loadingCount + 1
            }
        case GlobalTypes.HIDE_LOADING:
            return {
                ...state,
                loadingCount: state.loadingCount - 1
            }
        case GlobalTypes.RESET_LOADING:
            return {
                ...state,
                loadingCount: 0
            }
        case GlobalTypes.UPDATE_PERMISSION:
            return {
                ...state,
                permissions: data
            }
        case GlobalTypes.UPDATE_ROUTES:
            return {
                ...state,
                routes: data
            }
        case GlobalTypes.UPDATE_SIDE_ROUTES:
            return {
                ...state,
                sideMenuRoutes: data
            }
        case GlobalTypes.UPDATE_HOME:
            return {
                ...state,
                homeRoute: data
            }
        case GlobalTypes.UPDATE_TENANT:
            return {
                ...state,
                tenantInfo: data,
                tenant: data.companyCode
            }
        case GlobalTypes.UPDATE_FLAT_ROUTES:
            return {
                ...state,
                flatRoutes: data
            }
        default:
            return state;
    }
}